package com.jacklin.market.coupon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jacklin.market.coupon.domain.UserCoupon;
import com.jacklin.market.coupon.mapper.UserCouponMapper;
import com.jacklin.market.coupon.service.UserCouponService;
import org.jacklin.common.api.vo.Result;
import org.springframework.stereotype.Service;

/**
 * 营销-用户优惠券服务层实现类
 *
 * @author: jacklin
 * @since: 2022/3/26 15:58
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {


    public Result<?> checkUserCoupon(String couponCode) {
        UserCoupon userCoupon = this.lambdaQuery().eq(UserCoupon::getCouponCode, couponCode).one();
        if (userCoupon != null) {
            Integer couponStatus = userCoupon.getCouponStatus();
            if (1 == couponStatus) {
                return Result.error("该优惠券已使用");
            }
        } else {
            return Result.error("该优惠券不存在");
        }
        return Result.OK().success("校验成功");
    }

    @Override
    public boolean updateUserCouponStatus(String userId, String couponCode) {
        return baseMapper.updateUserCouponStatus(userId, couponCode);
    }
}
